package com.dl.mood.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.dl.mood.R;
import com.dl.mood.adapter.MoodListAdapter;
import com.dl.mood.databinding.LayoutMoodBinding;

import com.dl.mood.model.MoodItem;
import com.dl.mood.viewmodel.MoodViewModel;

import java.util.List;

public class MoodActivity extends BaseActivity {
    public static final String TAG = "MoodActivity";

    private LayoutMoodBinding binding;
    private MoodViewModel moodViewModel;
    private MoodListAdapter moodListAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        Window window = getWindow();
//        WindowCompat.setDecorFitsSystemWindows(window, true);
        moodViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(MoodViewModel.class);
//        moodViewModel2 = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(MoodViewModel2.class);
        binding = DataBindingUtil.setContentView(this, R.layout.layout_mood);


        binding.addBtn.setOnClickListener(v -> {
            Intent in = new Intent(MoodActivity.this, AddMoodActivity.class);
            startActivity(in);
        });

        binding.deleteAllBtn.setOnClickListener(v -> {
            moodViewModel.deleteAll();
            Toast.makeText(getApplicationContext(), "删除所有心情数据", Toast.LENGTH_SHORT).show();
        });

        binding.findEdt.setOnTouchListener((view, event) -> {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if ("搜索".equals(binding.findEdt.getHint())) {
                    binding.findEdt.setHint("");
                    // 手动请求焦点和显示软键盘
                    binding.findEdt.requestFocus();
                    binding.findEdt.dispatchTouchEvent(event);
                }
            }
            return false;
        });

        binding.findEdt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                String input = binding.findEdt.getText().toString();
                if (!input.isEmpty()) {
                    // 这里可以添加搜索逻辑
                    Toast.makeText(getApplicationContext(), "你输入的搜索内容是: " + input, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "请输入搜索内容", Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });


        moodListAdapter = new MoodListAdapter();
        binding.moodList.setLayoutManager(new LinearLayoutManager(this));
        binding.moodList.setAdapter(moodListAdapter);

        moodViewModel.getMoodItemList().observe(this, new Observer<List<MoodItem>>() {
            @Override
            public void onChanged(List<MoodItem> moodItems) {
                moodListAdapter.setMoodItems(moodItems);
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();
        moodViewModel.loadMoodItems(); // 重新加载数据
    }
}
